//
// Copyright (c) 2024 BPer
//
#ifndef __BPerAudioEncoderAac__
#define __BPerAudioEncoderAac__
#include <bperutil/buffer/BPerAudioEncoderBuffer.h>
#include <bperencoder/BPerAudioEncoder.h>
#include <bperutil/buffer/BPerAudioBuffer.h>
#include <bperutil/sys/BPerLoadLib.h>
#include <faac.h>


class BPerAudioEncoderAac: public BPerAudioEncoder
{
public:
	BPerAudioEncoderAac();
	~BPerAudioEncoderAac(void);
	void init(BPerAudioInfo *pap);
	int32_t encoder(BPerFrame* pframe,BPerEncoderCallback* pcallback);

private:
	uint8_t *m_aacBuffer;

	int32_t frames;
	int32_t mnInputSamples;
	uint32_t  isamples, maxsample;
	unsigned long nSampleRate;
	uint32_t  nChannels;
	uint32_t  nPCMBitSize;
	unsigned long nInputSamples;
	unsigned long nMaxOutputBytes;

	int32_t nRet;
	int32_t nBytesRead;
	int32_t nPCMBufferSize;

private:
	BPerLoadLib m_lib;
	void encoder(int32_t *p_buf);
	void closeAac();
	void saveWave();
	int32_t ret;
	int32_t isRec;
	faacEncHandle hEncoder;
	void loadLib();
	void unloadLib();
	faacEncHandle  (*bPer_faacEncOpen)(unsigned long sampleRate, uint32_t  numChannels,
			unsigned long *inputSamples,	unsigned long *maxOutputBytes);
	int32_t  (*bPer_faacEncSetConfiguration)(faacEncHandle hEncoder,faacEncConfigurationPtr config);
	int32_t  (*bPer_faacEncEncode)(faacEncHandle hEncoder, int32_t * inputBuffer, uint32_t  samplesInput,
				 uint8_t *outputBuffer, uint32_t  bufferSize);
	int32_t  (*bPer_faacEncClose)(faacEncHandle hEncoder);
	faacEncConfigurationPtr
	  (*bPer_faacEncGetCurrentConfiguration)(faacEncHandle hEncoder);
};

#endif
